package com.jic.order.controller.manage;

import com.alibaba.fastjson.JSON;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.RestResult;
import com.jic.common.base.vo.enums.DeptTypeEnums;
import com.jic.order.entity.OrderLogisticsCompany;
import com.jic.order.manage.request.OrderLogisticsCompanyEditRequest;
import com.jic.order.manage.request.OrderLogisticsCompanyRequest;
import com.jic.order.manage.response.ExpressMapConfigResponse;
import com.jic.order.manage.response.OrderLogisticsCompanyResponse;
import com.jic.common.base.vo.PageResult;
import com.jic.order.service.OrderLogisticsCompanyService;
import com.jic.order.utils.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@Api(tags = "配送公司设置")
@RequestMapping(value = "orderLogisticsCompany")
@RestController
public class OrderLogisticsCompanyController {

	@Autowired
	OrderLogisticsCompanyService orderLogisticsCompanyService;

	@ApiOperation(value = "获取配送公司信息")
	@PostMapping(value = "/queryPage")
	public RestResult<PageResult<OrderLogisticsCompanyResponse>> queryPage(@RequestBody OrderLogisticsCompanyRequest form) {
		log.info("开始调用配送公司分页，{}", JSON.toJSONString(form));
		Page page = new Page();
		page.setLimit(form.getLimit());
		page.setStart(form.getStart());
		OrderLogisticsCompany orderLogisticsCompany = new OrderLogisticsCompany();
		BeanUtils.copyProperties(form, orderLogisticsCompany);
		DeptTypeEnums deptType = form.getAdminOperate().getDeptType();
		if (deptType.getCode() != 0) {
			orderLogisticsCompany.setDeptId(form.getDeptId());
		}
		PageResult<OrderLogisticsCompany> result = orderLogisticsCompanyService.queryPage(orderLogisticsCompany, page);
		List<OrderLogisticsCompanyResponse> orderLogisticsCompanyResponseList = result.getRows().stream().map(temp -> {
			OrderLogisticsCompanyResponse orderLogisticsCompanyResponse = new OrderLogisticsCompanyResponse();
			BeanUtils.copyProperties(temp, orderLogisticsCompanyResponse);
			return orderLogisticsCompanyResponse;
		}).filter(item -> item != null).collect(Collectors.toList());
		PageResult<OrderLogisticsCompanyResponse> pageResult = new PageResult<>();
		pageResult.setRows(orderLogisticsCompanyResponseList);
		pageResult.setTotal(result.getTotal());
		return RestResult.success(pageResult);
	}

	@ApiOperation(value = "配送公司新增", notes = "新增配送公司")
	@PostMapping(value = "editOrderLogisticsCompany")
	public RestResult editOrderLogisticsCompany(@RequestBody OrderLogisticsCompanyEditRequest orderLogisticsCompanyEditRequest) {
		RestResult result = new RestResult();
		log.info("开始调用新增编辑配送公司，{}", JSON.toJSONString(orderLogisticsCompanyEditRequest));
		OrderLogisticsCompany orderLogisticsCompany = new OrderLogisticsCompany();
		BeanUtils.copyProperties(orderLogisticsCompanyEditRequest, orderLogisticsCompany);
		orderLogisticsCompany.setDeptId(orderLogisticsCompanyEditRequest.getAdminOperate().getDeptId());
		try {
			if (orderLogisticsCompanyEditRequest.getId() != null) {//修改
				result = orderLogisticsCompanyService.update(orderLogisticsCompany);
			} else {
				result = orderLogisticsCompanyService.insert(orderLogisticsCompany);
			}
		} catch (Exception e) {
			log.error("editOrderLogisticsCompany========》", e);
		}

		return result;
	}

	@ApiOperation(value = "删除配送公司")
	@PostMapping(value = "/deleteOrderLogisticsCompany")
	public RestResult deleteOrderLogisticsCompany(@RequestBody OrderLogisticsCompanyRequest orderLogisticsCompanyRequest) {
		log.info("开始调用删除配送公司规则，{}", orderLogisticsCompanyRequest.toString());
		RestResult restResult = new RestResult<>();
		try {
			restResult = orderLogisticsCompanyService.delete(orderLogisticsCompanyRequest);
		} catch (Exception e) {
			log.error("deleteOrderLogisticsCompany========》", e);
		}
		return restResult;

	}

	@ApiOperation(value = "系统快递公司初始化")
	@PostMapping(value = "/queryExpressMapConfig")
	public RestResult queryExpressMapConfig() {
		log.info("开始调用系统快递公司初始化，{}");
		RestResult restResult = new RestResult<>();
		try {
			List<ExpressMapConfigResponse> expressMapConfigResponseList = orderLogisticsCompanyService.queryExpressMapConfig();
			restResult.setData(expressMapConfigResponseList);
			restResult.setCode("0000");
			restResult.setMsg("调用成功");
		} catch (Exception e) {
			log.error("queryExpressMapConfig========》", e);
		}
		return restResult;

	}

	@ApiOperation(value = "获取配送公司信息")
	@PostMapping(value = "/queryList")
	public RestResult queryList() {
		OrderLogisticsCompany orderLogisticsCompany = new OrderLogisticsCompany();
		List<OrderLogisticsCompany> list = orderLogisticsCompanyService.query(orderLogisticsCompany);
		List<OrderLogisticsCompanyResponse> list1 = new ArrayList<>();
		list.forEach(item -> {
			list1.add(BeanUtil.copy(item, OrderLogisticsCompanyResponse.class));
		});
		return RestResult.success(list1);
	}

}
